package com.cn.algorithm02.class12;

import java.util.LinkedList;
import java.util.Queue;

/***
 * @author: hels
 * @description: 二叉树按层遍历
 **/
public class C01_LevelTraversalBT {
    public static void main(String[] args) {

        Node d = new Node("d", null, null);
        Node c = new Node("c", d, null);
        Node b1 = new Node("b1", null, null);
        Node b2 = new Node("b2", null, null);
        Node b = new Node("b", b1, b2);
        Node a = new Node("a", b, c);
        levelTraversal(a);
    }

    public static void levelTraversal(Node head) {
        if (head == null) return;
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.print(cur.value + " ");
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }

    }
}
